package com.compass.doctor.dialog

import android.app.Dialog
import android.os.Build
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.fragment.app.FragmentActivity
import com.compass.doctor.databinding.DialogPrivacyBinding
import com.compass.doctor.ui.WebDetailActivity
import com.compass.framework.R

import com.compass.framework.base.BaseDialog
import com.compass.framework.base.BaseDialogFragment
import com.compass.framework.databinding.DialogMessageBinding
import com.compass.framework.ext.onClick
import com.compass.framework.manager.AppManager
import com.compass.framework.utils.LogUtil
import com.compass.framework.utils.getColorFromResource
import com.compass.framework.utils.getStringFromResource

/**
 * Description： 意思协议弹窗
 * Date:2023/11/21 15:49
 */
class PrivacyDialog {

    class Builder(activity: FragmentActivity) : BaseDialogFragment.Builder<Builder>(activity) {

        private val mBinding: DialogPrivacyBinding =
            DialogPrivacyBinding.inflate(LayoutInflater.from(activity))
        init {
            initView()
        }

        private fun initView() {
            setContentView(mBinding.root)
            setWidth((AppManager.getScreenWidthPx() * 0.8).toInt())
            setAnimStyle(BaseDialog.AnimStyle.DEFAULT)
            setGravity(Gravity.CENTER)
            mBinding.tvDialogMessageTitle.text = getStringFromResource(R.string.dialog_tips_guide)
            mBinding.tvDialogMessageConfirm.text = getStringFromResource(R.string.default_confirm)
            mBinding.tvDialogMessageCancel.text = getString(R.string.default_cancel)
            initAgreement()


        }

        fun setonConfirmListener(onConfirm: (dialog: Dialog?) -> Unit): Builder {
            mBinding.tvDialogMessageConfirm.onClick {
                onConfirm?.invoke(dialog)
            }
            return this
        }

        fun setonCancelListener(onCancel: (dialog: Dialog?) -> Unit): Builder {
            mBinding.tvDialogMessageCancel.onClick {
                onCancel?.invoke(dialog)
            }
            return this
        }



        /**
         * 初始化协议点击
         */
        private fun initAgreement() {
            val agreement = getStringFromResource(R.string.splash_agreement)
            try {
                //设置点击事件，加上这句话才有效果
                mBinding.tvDialogMessageMessage.movementMethod = LinkMovementMethod.getInstance()
                val spaBuilder = SpannableStringBuilder(agreement)
                val privacySpan = getStringFromResource(R.string.login_privacy_agreement)
                val serviceSpan = getStringFromResource(R.string.login_user_agreement)
                spaBuilder.setSpan(
                    object : ClickableSpan() {
                        override fun onClick(widget: View) {
                            (widget as TextView).highlightColor =
                                getColorFromResource(R.color.transparent)

                            WebDetailActivity.start(
                                context = activity,
                                url = com.compass.framework.constant.POLICY,
                                title = "隐私政策")

                        }

                        @RequiresApi(Build.VERSION_CODES.M)
                        override fun updateDrawState(ds: TextPaint) {
                            super.updateDrawState(ds)
                            ds.color = getColorFromResource(R.color.color_0165b8)
                            ds.isUnderlineText = false
                            ds.clearShadowLayer()
                        }
                    },
                    spaBuilder.indexOf(privacySpan),
                    spaBuilder.indexOf(privacySpan) + privacySpan.length,
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                )
                spaBuilder.setSpan(
                    object : ClickableSpan() {
                        override fun onClick(widget: View) {
                            (widget as TextView).highlightColor =
                                getColorFromResource(R.color.transparent)
                            WebDetailActivity.start(
                                context = activity,
                                url = com.compass.framework.constant.SERVICE,
                                title = "服务协议"
                            )
                        }

                        @RequiresApi(Build.VERSION_CODES.M)
                        override fun updateDrawState(ds: TextPaint) {
                            super.updateDrawState(ds)
                            ds.color = getColorFromResource(R.color.color_0165b8)
                            ds.isUnderlineText = false
                            ds.clearShadowLayer()
                        }
                    },
                    spaBuilder.indexOf(serviceSpan),
                    spaBuilder.indexOf(serviceSpan) + serviceSpan.length,
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                )
                mBinding.tvDialogMessageMessage.setText(spaBuilder, TextView.BufferType.SPANNABLE)
            } catch (e: Exception) {
                LogUtil.e(e)
                mBinding.tvDialogMessageMessage.text = agreement
            }
        }
    }
}